package com.xspace.android.xafcommon.db.dbsub;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.xspace.android.xafcommon.db.BaseSql;
import com.xspace.android.xafcommon.db.SDBaseSql;

/**
 * CityDB数据库
 * 
 * @author pc-1
 * 
 */
public class AudioMsgDb extends BaseSql {

    public static final String TAG = "AudioMsgDb";
    public static final String TABLE_NAME = "AudioMsgDb";

	public static final String  AUDIOPATH= "audioPath";// 音频名称
	public static final String AUDIOURL = "audioUrl";// 音频路径
	public static final String DOWNTIME = "downTime";// 下载时间

	public static final String CREATE_TABLE = "create table IF NOT EXISTS " + TABLE_NAME
			+ "(" + "_id INTEGER primary key autoincrement ," 
			+ AUDIOPATH + " varchar(64) ,"
			+ AUDIOURL + " varchar(128) ,"
			+ DOWNTIME + " Long);";
	public AudioMsgDb(MyDBHelper db) {
		super(db);
	}

	public AudioMsgDb() {

	}

	@Override
	public void create(SQLiteDatabase db) {
		db.execSQL(CREATE_TABLE);

	}

	public void dropTable(SQLiteDatabase db) {
		db.execSQL(TB_DELETE + TABLE_NAME);
	}

	@Override
	public void upgrade(SQLiteDatabase db) {
	}

	public void insertInTransaction(String audioPath,String audioUrl) {
		values = new ContentValues();
		values.put(AUDIOPATH, audioPath);
		values.put(AUDIOURL, audioUrl);
		values.put(DOWNTIME, com.xspace.android.xafcommon.util.DateUtils.getNowDateTime());
        String sql = "select * from " + TABLE_NAME + " where " + AUDIOURL + " = '" + audioUrl + "'";
        System.out.println("------------------------------sql:"+sql);
        try {
            long rowId;
            Cursor  cursor = db.query(sql, null);
            if (cursor != null && cursor.getCount() > 0) {
                rowId = db.update(TABLE_NAME, values, AUDIOURL + "='"+audioUrl+"'");
            } else {
                rowId = super.saveOrUpdate(TABLE_NAME);
                Log.e(TAG, "rowId" + rowId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	}

	public String findAudioPath(String audioUrl) {
        String  audioPath = "";
        String sql = "select * from " + TABLE_NAME + " where " + AUDIOURL + "='"+audioUrl+"'";
        try {
        	Cursor  cursor = db.query(sql, null);
            if (cursor != null && cursor.getCount() > 0) {
              cursor.moveToNext();
                audioPath = cursor.getString(cursor.getColumnIndex(AUDIOPATH));
            }
        } catch (Exception e) {
			e.printStackTrace();

		} finally {
			db.close();
		}
       
        return audioPath;
	}
}
